package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.TeamMemberItemBean;
import sql.SprintViewDao;

/**
 * Servlet implementation class CancelSprint
 */
@WebServlet("/CancelSprint")
public class CancelSprint extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CancelSprint() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int sprintId = 0;

		try {
			sprintId = Integer.parseInt(request.getParameter("sprint-id"));
		}
		catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		HttpSession session = request.getSession(true);		
		TeamMemberItemBean currentUserInfo = (TeamMemberItemBean) session.getAttribute("currentUser");
		
		SprintViewDao sprintViewDao = new SprintViewDao();
		
		int userStoriesMovedToBacklog = 0;
		try {
			userStoriesMovedToBacklog = sprintViewDao.cancelSprint(currentUserInfo.getProjectId(), sprintId);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		
		PrintWriter out = response.getWriter();
		out.write("{ \"user_stories_moved_to_backlog\" : " + userStoriesMovedToBacklog + " }");
	}

}
